<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once('smarty/Smarty.class.php');

class smarty_wrapper extends Smarty {
    
    const VIEWS_COMPILATION_DIR = 'views_c';
    const VIEWS_COMPILATION_ADMIN_DIR = 'views_c/admin_views_c';
    const VIEWS_COMPILATION_STAND_ALONE = 'views_c/stand_alone_views_c';
    const VIEWS_EXT = '.tpl';

    private $master;
    private $base_url;
    private $ci;

    function __construct() {
        $this->Smarty();

        $this->left_delimiter = "{{";
        $this->right_delimiter = "}}";
        $this->compile_check = true;        
        $this->ci =& get_instance();
        
        $this->debugging = is_smarty_console__on();
        

        foreach($this->ci->modules as $curr_mod){            
            if(is_dir(APPPATH . "modules/$curr_mod->name/smarty_plugins")){                
                $this->plugins_dir[] = APPPATH . "modules/$curr_mod->name/smarty_plugins";
            }
        }
        
    }

    public function is_admin_theme(){
        return $this->ci->theme==='admin';
    }
    public function is_default_theme(){
        return $this->ci->theme==='default' ;
    }
    public function is_default_master(){
        return $this->master==='default';
    }

    public function view($view_name) {
        $this->compile_dir = APPPATH . self::VIEWS_COMPILATION_DIR;
        if($this->is_default_theme()){
            $this->template_dir = APPPATH."modules/core/views/";
            $this->display(strtolower($this->master) . self::VIEWS_EXT);
        }else{
            $theme = $this->get_theme();
            if(file_exists(APPPATH . "views/$theme/".strtolower($this->master) . self::VIEWS_EXT)){
                $this->template_dir = APPPATH."views/$theme/";
                $this->display(strtolower($this->master) . self::VIEWS_EXT);
            }else{
                $this->template_dir = APPPATH."views/$theme/";
                $this->master = "default";
                $this->display("../../modules/core/views/".strtolower('default') . self::VIEWS_EXT);
            }
        }
    }

    public function admin_view($view_name){
        $this->compile_dir = APPPATH . self::VIEWS_COMPILATION_ADMIN_DIR;
        $this->template_dir = APPPATH."modules/core/admin_views/";        
        $this->display(strtolower($this->master) . self::VIEWS_EXT);
    }
    
    public function set_master($name) {
        $this->master = $name;
    }

    public function get_current_mmodule(){
        return $this->ci->current_module;
    }

    public function set_theme($theme){
        if($theme==='default'){
            $this->master = "default";
        }elseif($theme=='admin'){
            $this->master = "default";
        }else{
            $this->master = $theme;
        }
    }
    public function get_theme(){
        return $this->ci->theme;
    }

    public function stand_alone_fetch($module,$view_file,$view_folder="views"){
        $this->compile_dir = APPPATH . self::VIEWS_COMPILATION_STAND_ALONE;
        $this->template_dir = APPPATH."modules/$module/$view_folder/";
        return $this->fetch( $view_file);
    }


}
?>